Skip to content

Fix use after move#3343

Merged
angeloskath merged 3 commits intomainfrom
use-after-move
Mar 31, 2026
Merged

Fix use after move#3343
angeloskath merged 3 commits intomainfrom
use-after-move

Conversation

@angeloskath
Copy link
Copy Markdown
Member

Well there is 1 very real bug and then the rest are officially bugs but don't really show up.

Basically I came across the Broadcast use after move now that I was looking into #3319 and I went to a small quest to find more. Well good news I didn't quite find many.

The main pattern that is questionable is taking a reference to a shape and then moving the array. I think officially this is undefined behavior. The change here is actually incurring no overhead since it copies the shape but then moves it to the array constructor.

@angeloskath
Copy link
Copy Markdown
Member Author

Oh I see it was already fixed on main sorry for the noise. Well I 'll leave the PR as it does one less copy on that bug and also fixes the other asymptomatic ones.

@angeloskath angeloskath requested a review from zcbenz March 31, 2026 08:09
Copy link
Copy Markdown
Collaborator

@zcbenz zcbenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@zcbenz
Copy link
Copy Markdown
Collaborator

zcbenz commented Mar 31, 2026

With SmallVector there is actually not much meaning to move shapes and strides since they no longer allocate on heap (unless it has very large ndim which is forbidden in CUDA backend), so std::move would just add overhead erasing the old object's memory to 0s.

@angeloskath
Copy link
Copy Markdown
Member Author

Interesting... force of habit I guess. Do you think we should remove the moves of Shape and Strides throughout ?

There is probably more than 100 places.

$ grep -nr 'std::move(shape)' mlx | wc -l
      52

@angeloskath angeloskath merged commit 8439b1f into main Mar 31, 2026
16 checks passed
@angeloskath angeloskath deleted the use-after-move branch March 31, 2026 17:37
@zcbenz
Copy link
Copy Markdown
Collaborator

zcbenz commented Mar 31, 2026

I think it does not worth the effort changing all existing code as it is not going to make much difference in performance, we should just avoid introducing new ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants